package com.open.pokergames.biz.views.groups

import android.os.Bundle
import android.view.View
import com.jiangyy.easydialog.CommonDialog
import com.open.pokergames.R
import com.open.pokergames.base.activity.EventBusTitleBaseActivity
import com.open.pokergames.biz.entity.GroupVo
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.groups.constant.GroupData
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_group_add.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GroupAddActivity : EventBusTitleBaseActivity(), View.OnClickListener {

    private var viewModel: BizViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_group_add)
        setViewTitle("创建俱乐部")
        viewModel = BizViewModel()

        setListener()
    }

    private fun setListener() {
        doBtn.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v) {
            doBtn -> {
                CommonDialog.Builder(this)
                    .setCancelable(false)
                    .setTitle("重要提示")
                    .setMessage("创建俱乐部将扣除${GroupData.ADD_GROUP_CARDS}张房卡，确定创建吗？")
                    .setPositiveButton("确定") {
                        doAdd()
                    }.setNegativeButton("取消") {
                    }.show()
            }
        }
    }

    private fun doAdd() {
        val groupName = groupNameEt.text.toString()
        val address = addressEt.text.toString()
        val linkType = linkTypeEt.text.toString()
        val linkValue = linkValueEt.text.toString()
        val groupFlags = groupFlagsEt.text.toString()
        val groupDesc = groupDescEt.text.toString()

        val groupVo = GroupVo()
        if (XUtils.string().isEmpty(groupName)) {
            XUtils.toast().show("群名称不能为空")
            return
        } else {
            groupVo.groupName = groupName
        }

        if (XUtils.string().isEmpty(address)) {
            XUtils.toast().show("地区不能为空")
            return
        } else {
            groupVo.address = address
        }

        if (XUtils.string().isEmpty(linkType)) {
            XUtils.toast().show("联系渠道不能为空")
            return
        } else {
            groupVo.linkType = linkType
        }

        if (XUtils.string().isEmpty(linkValue)) {
            XUtils.toast().show("联系方式不能为空")
            return
        } else {
            groupVo.linkValue = linkValue
        }

        if (XUtils.string().isEmpty(groupFlags)) {
            XUtils.toast().show("俱乐部标签不能为空")
            return
        } else {
            groupVo.groupFlags = groupFlags
        }

        if (XUtils.string().isEmpty(groupDesc)) {
            XUtils.toast().show("俱乐部简介不能为空")
            return
        } else {
            groupVo.groupDesc = groupDesc
        }

        showLoadingDialog("正在创建")
        viewModel?.addGroup(groupVo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun callback(resultInfo: ResultInfo<String>) {

        if (resultInfo.eventType != "addGroup") {
            return
        }

        hideLoadingDialog()

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("创建成功")
                finish()
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
            }
        }
    }


}